/*
 * GantiPasswordView.java
 *
 * Created on September 7, 2008, 10:58 PM
 */
package com.indragunawan.restobiz.app;

import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author  igoens
 */
public class GantiPasswordView extends javax.swing.JDialog {

    private static final long serialVersionUID = -7723055877946217869L;

    GeneralConfig cfg = new GeneralConfig();

    /** Creates new form GantiPasswordView */
    public GantiPasswordView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        /** Set user header */
        setNamaUserField();
    }

    private void tutupGantiPassword() {
        this.dispose();
    }

    private void resetPasswordForm() {
        passwordBaruField.setText("");
        passwordLamaField.setText("");
        passwordKonfirmasiField.setText("");
        passwordBaruField.requestFocus();
    }

    private void setNamaUserField() {
        namaUserField.setText(cfg.getLoggedUserName() + " (" + cfg.getLoggedUser() + ")");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPanel = new javax.swing.JPanel();
        passwordLamaLabel = new javax.swing.JLabel();
        passwordBaruLabel = new javax.swing.JLabel();
        passwordKonfirmasiLabel = new javax.swing.JLabel();
        passwordLamaField = new javax.swing.JPasswordField();
        passwordBaruField = new javax.swing.JPasswordField();
        passwordKonfirmasiField = new javax.swing.JPasswordField();
        headerPanel = new javax.swing.JPanel();
        namaUserField = new javax.swing.JLabel();
        footerPanel = new javax.swing.JPanel();
        gantiButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getResourceMap(GantiPasswordView.class);
        setTitle(resourceMap.getString("gantiPassword.title")); // NOI18N
        setName("gantiPassword"); // NOI18N
        setResizable(false);

        contentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        contentPanel.setName("contentPanel"); // NOI18N

        passwordLamaLabel.setDisplayedMnemonic('L');
        passwordLamaLabel.setLabelFor(passwordLamaField);
        passwordLamaLabel.setText(resourceMap.getString("passwordLamaLabel.text")); // NOI18N
        passwordLamaLabel.setName("passwordLamaLabel"); // NOI18N

        passwordBaruLabel.setDisplayedMnemonic('o');
        passwordBaruLabel.setLabelFor(passwordBaruField);
        passwordBaruLabel.setText(resourceMap.getString("passwordBaruLabel.text")); // NOI18N
        passwordBaruLabel.setName("passwordBaruLabel"); // NOI18N

        passwordKonfirmasiLabel.setDisplayedMnemonic('K');
        passwordKonfirmasiLabel.setLabelFor(passwordKonfirmasiField);
        passwordKonfirmasiLabel.setText(resourceMap.getString("passwordKonfirmasiLabel.text")); // NOI18N
        passwordKonfirmasiLabel.setName("passwordKonfirmasiLabel"); // NOI18N

        passwordLamaField.setText(resourceMap.getString("passwordLamaField.text")); // NOI18N
        passwordLamaField.setName("passwordLamaField"); // NOI18N
        passwordLamaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordLamaFieldKeyPressed(evt);
            }
        });

        passwordBaruField.setText(resourceMap.getString("passwordBaruField.text")); // NOI18N
        passwordBaruField.setName("passwordBaruField"); // NOI18N
        passwordBaruField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordBaruFieldKeyPressed(evt);
            }
        });

        passwordKonfirmasiField.setText(resourceMap.getString("passwordKonfirmasiField.text")); // NOI18N
        passwordKonfirmasiField.setName("passwordKonfirmasiField"); // NOI18N
        passwordKonfirmasiField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordKonfirmasiFieldKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passwordLamaField, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addComponent(passwordKonfirmasiLabel)
                    .addComponent(passwordLamaLabel)
                    .addComponent(passwordBaruLabel)
                    .addComponent(passwordBaruField, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addComponent(passwordKonfirmasiField, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                .addContainerGap())
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(passwordLamaLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordLamaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordBaruLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordBaruField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordKonfirmasiLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordKonfirmasiField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        headerPanel.setBackground(resourceMap.getColor("headerPanel.background")); // NOI18N
        headerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        headerPanel.setName("headerPanel"); // NOI18N

        namaUserField.setFont(resourceMap.getFont("namaUserField.font")); // NOI18N
        namaUserField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        namaUserField.setText(resourceMap.getString("namaUserField.text")); // NOI18N
        namaUserField.setName("namaUserField"); // NOI18N

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(namaUserField, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                .addContainerGap())
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(namaUserField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        footerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        footerPanel.setName("footerPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getActionMap(GantiPasswordView.class, this);
        gantiButton.setAction(actionMap.get("gantiPassword")); // NOI18N
        gantiButton.setMnemonic('G');
        gantiButton.setText(resourceMap.getString("gantiButton.text")); // NOI18N
        gantiButton.setName("gantiButton"); // NOI18N
        gantiButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                gantiButtonKeyPressed(evt);
            }
        });

        batalButton.setAction(actionMap.get("batalGantiPassword")); // NOI18N
        batalButton.setMnemonic('B');
        batalButton.setText(resourceMap.getString("batalButton.text")); // NOI18N
        batalButton.setName("batalButton"); // NOI18N
        batalButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                batalButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout footerPanelLayout = new javax.swing.GroupLayout(footerPanel);
        footerPanel.setLayout(footerPanelLayout);
        footerPanelLayout.setHorizontalGroup(
            footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPanelLayout.createSequentialGroup()
                .addContainerGap(124, Short.MAX_VALUE)
                .addComponent(gantiButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(batalButton)
                .addContainerGap())
        );

        footerPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {batalButton, gantiButton});

        footerPanelLayout.setVerticalGroup(
            footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(batalButton)
                    .addComponent(gantiButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(footerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(footerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void passwordLamaFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordLamaFieldKeyPressed

    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_passwordLamaFieldKeyPressed

private void passwordBaruFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordBaruFieldKeyPressed

    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_passwordBaruFieldKeyPressed

private void passwordKonfirmasiFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordKonfirmasiFieldKeyPressed

    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_passwordKonfirmasiFieldKeyPressed

private void gantiButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_gantiButtonKeyPressed

    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        gantiPassword();
    }
}//GEN-LAST:event_gantiButtonKeyPressed

private void batalButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_batalButtonKeyPressed

    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        batalGantiPassword();
    }
}//GEN-LAST:event_batalButtonKeyPressed

    @Action
    public void batalGantiPassword() {
        tutupGantiPassword();
    }

    @Action
    @SuppressWarnings("empty-statement")
    public void gantiPassword() {
        String oldPassword = null;
        String newPassword = null;
        String confirmPassword = null;

        oldPassword = String.valueOf(passwordLamaField.getPassword());
        newPassword = String.valueOf(passwordBaruField.getPassword());
        confirmPassword = String.valueOf(passwordKonfirmasiField.getPassword());

        /** Check connection and display warning if not available */
        if (cfg.confirmConnected()) {
            /** Check if current user is valid user */
            if (cfg.isValidUser(cfg.getLoggedUser(), oldPassword)) {
                if (newPassword.equals(confirmPassword)) {
                    if (JOptionPane.showConfirmDialog(this, "Ganti password untuk user: " + cfg.getLoggedUser() + "?", "Konfirmasi", JOptionPane.YES_OPTION) == JOptionPane.YES_OPTION) {
                        try {
                            cfg.setUserPassword(cfg.getLoggedUser(), newPassword);
                            JOptionPane.showMessageDialog(null, "Ganti password berhasil");
                            tutupGantiPassword();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
                            JOptionPane.showMessageDialog(null, "Ganti password gagal");
                        }
                        resetPasswordForm();
                    }
                    ;
                } else {
                    JOptionPane.showMessageDialog(null, "Password baru dan konfirmasi tidak sesuai");
                    passwordBaruField.requestFocus();
                }
            } else {
                JOptionPane.showMessageDialog(null, "Password lama tidak sesuai");
                passwordLamaField.requestFocus();
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalButton;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel footerPanel;
    private javax.swing.JButton gantiButton;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel namaUserField;
    private javax.swing.JPasswordField passwordBaruField;
    private javax.swing.JLabel passwordBaruLabel;
    private javax.swing.JPasswordField passwordKonfirmasiField;
    private javax.swing.JLabel passwordKonfirmasiLabel;
    private javax.swing.JPasswordField passwordLamaField;
    private javax.swing.JLabel passwordLamaLabel;
    // End of variables declaration//GEN-END:variables
}
